﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Tilver.Common;
using Weixin.MP.Map.Models;

namespace Weixin.MP.Map.Controllers
{
    public class WeatherController : Controller
    {

        #region 台风地图
        
        public JavaScriptResult GetTyphoon()
        {
            string s = "{ \"Code\": \"1416\", \"Name\": \"凤凰\", \"EName\": \"FUNG-WONG\", \"StartDate\": \"2014-09-18T02:00:00\", \"EndDate\": \"2014-09-21T23:00:00\",\"Points\": [{ \"StrTime\": \"2014年09月18日02时\", \"Longitude\": 129.6, \"Latitude\": 13.2, \"Strong\": \"热带风暴\", \"Power\": 8, \"Speed\": 18, \"Moves\": \"西北\", \"MovingSpeed\": \"30\", \"Pressure\": 998, \"Radius7\": 200, \"Radius10\": null, \"Radius12\": null, \"Forecasts\": null },{ \"StrTime\": \"2014年09月18日05时\", \"Longitude\": 128.9, \"Latitude\": 13.2, \"Strong\": \"热带风暴\", \"Power\": 8, \"Speed\": 18, \"Moves\": \"西北\", \"MovingSpeed\": \"27\", \"Pressure\": 998, \"Radius7\": 200, \"Radius10\": null, \"Radius12\": null, \"Forecasts\": null },{ \"StrTime\": \"2014年09月18日08时\", \"Longitude\": 128.3, \"Latitude\": 13.4, \"Strong\": \"热带风暴\", \"Power\": 8, \"Speed\": 18, \"Moves\": \"西北\", \"MovingSpeed\": \"30\", \"Pressure\": 998, \"Radius7\": 200, \"Radius10\": null, \"Radius12\": null, \"Forecasts\": null },{ \"StrTime\": \"2014年09月18日11时\", \"Longitude\": 127.9, \"Latitude\": 13.8, \"Strong\": \"热带风暴\", \"Power\": 8, \"Speed\": 18, \"Moves\": \"西北\", \"MovingSpeed\": \"30\", \"Pressure\": 998, \"Radius7\": 200, \"Radius10\": null, \"Radius12\": null, \"Forecasts\": null },{ \"StrTime\": \"2014年09月21日08时\", \"Longitude\": 120.2, \"Latitude\": 21.8, \"Strong\": \"强热带风暴\", \"Power\": 10, \"Speed\": 28, \"Moves\": \"北\", \"MovingSpeed\": \"25\", \"Pressure\": 982, \"Radius7\": 180, \"Radius10\": 50, \"Radius12\": null, \"Forecasts\": null },{ \"StrTime\": \"2014年09月21日09时\", \"Longitude\": 120.4, \"Latitude\": 21.8, \"Strong\": \"强热带风暴\", \"Power\": 10, \"Speed\": 28, \"Moves\": \"北\", \"MovingSpeed\": \"25\", \"Pressure\": 982, \"Radius7\": 180, \"Radius10\": 50, \"Radius12\": null, \"Forecasts\":[{ \"Sets\": \"日本\", \"Points\":[{ \"StrTime\": \"2014年09月21日08时\", \"Longitude\": 120.2, \"Latitude\": 21.8, \"Strong\": \"强热带风暴\", \"Power\": 10, \"Speed\": 28, \"Moves\": \"北\", \"MovingSpeed\": \"25\", \"Pressure\": 982, \"Radius7\": 180, \"Radius10\": 50, \"Radius12\": null, \"Forecasts\": null },{ \"StrTime\": \"2014年09月22日08时\", \"Longitude\": 121.5, \"Latitude\": 26.3, \"Strong\": \"热带风暴\", \"Power\": 9, \"Speed\": 23, \"Moves\": \"北\", \"MovingSpeed\": \"12KT\", \"Pressure\": 985, \"Radius7\": null, \"Radius10\": null, \"Radius12\": null, \"Forecasts\": null }]},{ \"Sets\": \"韩国\", \"Points\":[{ \"StrTime\": \"2014年09月21日08时\", \"Longitude\": 120.2, \"Latitude\": 21.8, \"Strong\": \"强热带风暴\", \"Power\": 10, \"Speed\": 28, \"Moves\": \"北\", \"MovingSpeed\": \"25\", \"Pressure\": 982, \"Radius7\": 180, \"Radius10\": 50, \"Radius12\": null, \"Forecasts\": null },{ \"StrTime\": \"2014年09月22日08时\", \"Longitude\": 120.9, \"Latitude\": 26.7, \"Strong\": \"热带风暴\", \"Power\": 9, \"Speed\": 24, \"Moves\": \"\", \"MovingSpeed\": \"\", \"Pressure\": 990, \"Radius7\": null, \"Radius10\": null, \"Radius12\": null, \"Forecasts\": null },{ \"StrTime\": \"2014年09月25日08时\", \"Longitude\": 131.8, \"Latitude\": 36.1, \"Strong\": null, \"Power\": null, \"Speed\": null, \"Moves\": \"\", \"MovingSpeed\": \"\", \"Pressure\": null, \"Radius7\": null, \"Radius10\": null, \"Radius12\": null, \"Forecasts\": null }]}]}]}";
            return JavaScript(s);
        }
        public ActionResult Typhoon(string stationnum)
        {
            try
            {

            }
            catch
            {
                
            }

            ViewBag.Lon = "120.82813";
            ViewBag.Lat = "29.594591";
            return View();
        }
        #endregion

        #region 天气地图

        public ActionResult Map(string stationnum, string weathertype)
        {
            try
            {
                int awsType = 0;
                if (weathertype.ToLower() == "visib")
                {
                    awsType = 1;
                }
                if (weathertype.ToLower() == "intensity")
                {
                    awsType = 2;
                }
                var appSetting = ConfigurationManager.AppSettings;
                var dataAccessType = appSetting.AllKeys.Contains("WeatherDataAccessType") ? appSetting["WeatherDataAccessType"] : "XML";
                if (dataAccessType.ToLower() == "xml")
                {
                    var filePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase +
                                   (appSetting.AllKeys.Contains("WeatherPath")
                                        ? appSetting["WeatherPath"]
                                        : @"Upload\Weather\weather.xml");
                    var o = Weixin.MP.Map.Models.XmlSerializer.LoadFromXml(filePath, typeof(Map.Models.Weixin), Encoding.GetEncoding("GBK"));
                    var jss = new JavaScriptSerializer();
                    ViewBag.WeatherJson = jss.Serialize(o);
                    ViewBag.WeatherType = weathertype;

                    //获取地区中心点
                    //var point = GetCenterPointDO(stationnum, awsType);
                    ViewBag.Lon = "120.82813";
                    ViewBag.Lat = "29.594591";
                }
                if (dataAccessType.ToLower() == "db")
                {
                    //var stationNum = appSetting.AllKeys.Contains("WeatherStationNum") ? appSetting["WeatherStationNum"] : "";
                    var stationNum = stationnum;
                    var xml = GetWeatherXMLDO(stationNum, awsType);
                    var o = Weixin.MP.Map.Models.XmlSerializer.LoadFromXml(xml, typeof(Map.Models.Weixin));
                    var jss = new JavaScriptSerializer();
                    ViewBag.WeatherJson = jss.Serialize(o);
                    ViewBag.WeatherType = weathertype;

                    //获取地区中心点
                    var point = GetCenterPointDO(stationnum, awsType);
                    ViewBag.Lon = point.Item1;
                    ViewBag.Lat = point.Item2;
                }
               
            }
            catch
            {
            }

            return View();
        }

        private Tuple<string, string> GetCenterPointDO(string stationNum, int aws_type)
        {
            return WebCache.Get(WebCache.WeatherCenterPoint + stationNum + aws_type.ToString(), objects => GetCenterPoint(stationNum, aws_type), 120);
        }
        private Tuple<string, string> GetCenterPoint(string stationNum, int aws_type)
        {
            var mo = new Weixin.MP.DAL.CoWeixin.Coder用户管理MO("WeatherWeixinMP");
            var eo = mo.SelectByCoder站号(stationNum, 1, "").FirstOrDefault();
            if (eo != null)
            {
                if (aws_type == 0)
                {
                    return new Tuple<string, string>(eo.Coder东经.HasValue ? eo.Coder东经.Value.ToString() : "", eo.Coder北纬.HasValue ? eo.Coder北纬.Value.ToString() : "");
                }
                else
                {
                    return new Tuple<string, string>(eo.Coder东经1.HasValue ? eo.Coder东经1.Value.ToString() : "", eo.Coder北纬1.HasValue ? eo.Coder北纬1.Value.ToString() : "");
                }
            }
            else
            {
                return new Tuple<string, string>("", "");
            }
        }

        private string GetWeatherXMLDO(string stationNum, int aws_type)
        {
            return WebCache.Get(WebCache.WeatherXML + stationNum + aws_type.ToString(), objects => GetWeatherXML(stationNum, aws_type), 120);
        }
        /// <summary>
        /// 天气实况地图数据
        /// </summary>
        /// <param name="stationNum"></param>
        /// <param name="aws_type"></param>
        /// <returns></returns>
        private string GetWeatherXML(string stationNum, int aws_type)
        {
            string xml = string.Empty;
            var mo = new Weixin.MP.DAL.CoWeixin.Coder天气实况MO("WeatherWeixinMP");
            var para = new List<DbParameter>();
            {
                var p = new SqlParameter("@stationnum", SqlDbType.VarChar);
                p.Value = stationNum;
                para.Add(p);
            }
            {
                var p = new SqlParameter("@awsType", SqlDbType.Int);
                p.Value = aws_type;
                para.Add(p);
            }
            var eo = mo.Select(" stationnum=@stationnum and aws_type=@awsType ", 1, " id desc ", para).FirstOrDefault();
            if (eo != null)
            {
                xml = eo.Text;
            }
            return xml;

        }
        #endregion

    }
}
